import Vue from 'vue'
import Vuex from 'vuex'
import user from './moudles/user.js'
import setting from './moudles/setting.js'
Vue.use(Vuex)

export default new Vuex.Store({
  // 仓库
  state: {
    num: 1,
    name: 'jack',
    list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  },
  getters: {
    // 类似于compute属性
    sum (state) {
      return state.list.reduce((sum, item) => sum + item, 0)
    }
  },
  mutations: {
    // 对state的数据进行修改(唯一能修改仓库数据的地方)
    handle (state) {
      state.num += 1
    },
    handle1 (state, n) {
      state.num += n
    }
  },
  actions: {
    // 进行异步操作
    handle2 ({ commit }) {
      setTimeout(() => {
        commit('handle')
      }, 1000)
    },
    handle3 ({ commit }, m) {
      setTimeout(() => {
        commit('handle1', m)
      }, 1000)
    }
  },
  modules: {
    // 进行模块化
    user,
    setting
  }
})
